package com.wds.exception;

import com.wds.domain.JsonResult;
import com.wds.domain.type.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * @author WDs , wds8.24@outlook.com
 * @version 1.0
 * @since 2022-10-27 9:33
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    /**
     *  自定义异常处理器
     *  将 message 写入日志 并返回
     */
    @ExceptionHandler(CustomException.class)
    public JsonResult<String> customExceptionHandler(Exception e){
        log.error("您有新的异常信息，亲 ：{}" ,e.getMessage());
        return JsonResult.fail(500, e.getMessage());

    }


    /**
     * 授权失败异常处理器
     */
    @ExceptionHandler(AccessDeniedException.class)
    public JsonResult<String> accessDeniedHandler(Exception e){
        log.error("授权失败：{}" ,e.getMessage());
        return JsonResult.http(AppHttpCodeEnum.NO_OPERATOR_AUTH);
    }

    /**
     * 文件大小超过限制
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public JsonResult<String> maxSize(Exception e){
        return JsonResult.http(AppHttpCodeEnum.UPLOAD_FAIL);
    }
}
